package com.diyunapp.xilinapp.fragments;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.GoodsBuyRecordsAdapter;
import com.diyunapp.xilinapp.bean.GoodsBuyRecordsBean;
import com.diyunapp.xilinapp.details.GoodsDetailActivity;
import com.diyunapp.xilinapp.utils.ScrollListViewDisplay;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class GoodDetailBuyRecordFragment extends Fragment {

    @Bind(R.id.goods_buyrecords_lv)
    ListView goodsBuyrecordsLv;
    private View myFragmentView;
    private List<GoodsBuyRecordsBean> bRlist;
    private GoodsBuyRecordsAdapter bRadapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        myFragmentView = inflater.inflate(R.layout.good_detail_buyrecord_fragment, null);
        ButterKnife.bind(this, myFragmentView);
        initViews();
        getBuyRecords();
        return myFragmentView;
    }

    private void initViews() {
        bRlist = new ArrayList<>();
        bRadapter = new GoodsBuyRecordsAdapter(bRlist,getActivity());
        goodsBuyrecordsLv.setAdapter(bRadapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getBuyRecords() {
        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=sale_record&goods_id=" + GoodsDetailActivity.good_id;
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(200);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int result = parseProject(responseInfo.result);
                        switch (result) {
                            case 1:
                                break;
                            case 0:
                                Toast.makeText(getContext(), "购买记录查询失败！", Toast.LENGTH_LONG).show();
                                break;
                            default:
                                break;
                        }
                    }


                    @Override
                    public void onStart() {

                    }

                    private int parseProject(String result) {
                        Log.i("zhangxiaoxidebug", "buyrecord_result=" + result);
                        try {
                            JSONObject jsObjectCode = new JSONObject(result);
                            int code = jsObjectCode.getInt("code");
                            if (code != 200) {
                                return 0;
                            }

                            String date = jsObjectCode.getString("date");
                            if(date.equals("is null"))
                            {
                                return 2;
                            }
                            JSONArray jsArrayDatas = new JSONArray(date);
                            for(int i=0;i<jsArrayDatas.length();i++)
                            {
                                JSONObject recordObject = jsArrayDatas.getJSONObject(i);
                                String buyer_name = recordObject.getString("buyer_name");
                                String goods_pay_price = recordObject.getString("goods_pay_price");
                                String goods_num = recordObject.getString("goods_num");
                                String add_time = recordObject.getString("add_time");
                                bRlist.add(new GoodsBuyRecordsBean("",buyer_name,goods_pay_price,goods_num,add_time));
                            }

                            bRadapter.notifyDataSetChanged();
                            ScrollListViewDisplay.setListViewHeightBasedOnChildren(goodsBuyrecordsLv);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 1;
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.i("zhangxiaoxidebug", "getIndex....onFailure msg=" + msg);
                        Toast.makeText(getContext(), "购买记录查询失败，请检查网络设置！", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }
}
